package com.sosee.cgfw.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.sosee.cgfw.R;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.wechat.utils.WechatHelper;

/**
 * Created by Thinkpad on 2016/1/25.
 * //新闻详情activity
 */
public class NewWebActivity extends Activity {
    WebView wv;
    private String title;//新闻标题
    private String url;//新闻网页链接
    private RelativeLayout rl;//分享选项
    private String text = "测试内同";//需要分享的内容
    private String imageurl;//分享的图片链接

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newweb);
        ShareSDK.initSDK(this);
        initView();
    }



    //初始化界面
    public void initView() {
        rl = (RelativeLayout) findViewById(R.id.rl_share);
        rl.setVisibility(View.INVISIBLE);
        wv = (WebView) findViewById(R.id.wv);
        String temUrl = getIntent().getStringExtra("url");
        LogUtils.log_E("测试 网站"+temUrl);
        if (temUrl != null) {
            if (temUrl.contains("http://") || temUrl.contains("https://")) {
                url = temUrl;
            } else {
                url = "http://" + temUrl;
            }
        }
//        url = "http://" + getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");
        text = getIntent().getStringExtra("text");
        imageurl = getIntent().getStringExtra("imageurl");

        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setUseWideViewPort(true);
        wv.setWebViewClient(new WebViewClient() {

        });
        wv.loadUrl(url);//加载url
    }

    //回退
    public void back(View view) {
        finish();
    }

    //分享新闻
    public void share(View view) {
        //分享功能待实现
        showShare();
    }

    //显示分享选项
    private void showShare() {
        rl.setVisibility(View.VISIBLE);
    }

    //分享
    public void share_chose(View view) {
        if (view.getId() == R.id.ig_weibo) {
            //微博分享
            weiboShare(new String[]{title, text, imageurl, url});
//            showShare(false,SinaWeibo.NAME);
//            showshare();
        } else {
            //微信朋友圈分享
            weiXinWebShare(false, this, new String[]{title, "ceshi", null, url});
        }

    }

    //返回键事件
    @Override
    public void onBackPressed() {
        if (rl.getVisibility() == View.VISIBLE) {
            //当分享选项出现时，点击回退键使其消失
            rl.setVisibility(View.INVISIBLE);
        } else {
            //当分享选项没有出现时，点击回退键直接退出当前activity
            finish();
        }
    }

    //微信分享
    private Context context;

    public void weiXinWebShare(boolean isWeixin, Context mContext, String... args) {
        if (null == mContext || args.length < 1) {
//            MainApplication.getIns().tip('分享失败');
            return;
        } else {
            this.context = mContext;
            for (int i = 0; i < args.length; i++) {
                String str = args[i];
                if (TextUtils.isEmpty(str) && i != 2) {
//                    MainApplication.getIns().tip('分享失败');分享失败
                    return;
                }
            }
            String title = args[0];
            String text = args[1];
            String image = args[2];
            String url = args[3];
            WechatHelper.ShareParams sp = null;
            if (isWeixin) {
                sp = new Wechat.ShareParams();
            } else {
                sp = new WechatMoments.ShareParams();
            }
            sp.title = title;

            if (TextUtils.isEmpty(image)) {
//                sp.imageData = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.logo_mingdao)).getBitmap();
            } else if (image.contains("http://") || image.contains("https://")) {
                sp.imageUrl = image;
            }
            sp.text = text;
            if (url.contains("http://") || url.contains("https://")) {
                sp.url = url;
            }
            sp.setShareType(Platform.SHARE_WEBPAGE);
            Platform plat = null;
            if (isWeixin) {
                plat = ShareSDK.getPlatform(Wechat.NAME);
            } else {
                plat = ShareSDK.getPlatform(WechatMoments.NAME);
            }
            plat.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                    Utils.showToast(getString(R.string.share_success));
                    rl.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {
//                    Toast.makeText(NewWebActivity.this,"朋友圈分享失败",Toast.LENGTH_LONG).show();
                    Utils.showToast(getString(R.string.share_failed));
                    rl.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onCancel(Platform platform, int i) {
//                    Toast.makeText(NewWebActivity.this,"分享取消",Toast.LENGTH_LONG).show();
                    Utils.showToast(getString(R.string.share_cancle));
                    rl.setVisibility(View.INVISIBLE);

                }
            });
            plat.share(sp);
        }
    }

    //新浪微博分享
    public void weiboShare(String... args) {

        SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        Platform weibo = ShareSDK.getPlatform(this,SinaWeibo.NAME);
        String title = args[0];
        String text = args[1];
        String url = args[3];
        sp.setTitle(title);
        sp.setUrl(url);
        sp.setText(text);
        weibo.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Utils.showToast(getString(R.string.share_success));
                rl.setVisibility(View.INVISIBLE);
            }
            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Utils.showToast(getString(R.string.share_failed));
                LogUtils.log_E("失败日志" + throwable.getMessage());
                rl.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Utils.showToast(getString(R.string.share_cancle));
                rl.setVisibility(View.INVISIBLE);
            }
        }); // 设置分享事件回调
            // 执行图文分享
        weibo.share(sp);




    }


    public void showshare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        //        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        oks.setImageUrl(url);
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        //        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        //        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);


    }


    private void showShare(boolean silent, String platform) {
        final OnekeyShare oks = new OnekeyShare();
        oks.setAddress("12345678901");
        oks.setTitle("来自男人装的分享");
        oks.setTitleUrl("http://sharesdk.cn");
        oks.setText("测试数据");
        // oks.setImagePath("http://img0.bdstatic.com/img/image/shouye/muzrxc1.jpg");//costume.getLargepic()
        oks.setUrl("http://www.sharesdk.cn");
        oks.setSite("男人装");
        oks.setSiteUrl("http://sharesdk.cn");
        oks.setVenueName("ShareSDK");
        oks.setSilent(silent);
        if (platform != null) {
            oks.setPlatform(platform);
        }
    // 去除注释，可令编辑页面显示为Dialog模式
    // oks.setDialogMode();
    // 去除注释，在自动授权时可以禁用SSO方式
    // oks.disableSSOWhenAuthorize();
    // 去除注释，则快捷分享的操作结果将通过OneKeyShareCallback回调
     oks.setCallback(new PlatformActionListener() {
         @Override
         public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
             Utils.showToast("分享成功");
         }

         @Override
         public void onError(Platform platform, int i, Throwable throwable) {
            Utils.showToast("分享失败");
             LogUtils.log_E("失败原因："+throwable.getMessage());
         }

         @Override
         public void onCancel(Platform platform, int i) {

         }
     });
//        oks.setShareContentCustomizeCallback(new ShareContentCustomizeDemo());
    // 去除注释，演示在九宫格设置自定义的图标
    // Bitmap logo = BitmapFactory.decodeResource(menu.getResources(), R.drawable.ic_launcher);
    // String label = menu.getResources().getString(R.string.app_name);
    // OnClickListener listener = new OnClickListener() {
    // public void onClick(View v) {
    // String text = "Customer Logo -- ShareSDK " + ShareSDK.getSDKVersionName();
    // Toast.makeText(menu.getContext(), text, Toast.LENGTH_SHORT).show();
    // oks.finish();
    // }
    // };
    // oks.setCustomerLogo(logo, label, listener);
    // 去除注释，则快捷分享九宫格中将隐藏新浪微博和腾讯微博
    // oks.addHiddenPlatform(SinaWeibo.NAME);
    // oks.addHiddenPlatform(TencentWeibo.NAME);
        oks.show(this);
    }



    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


}
